//
//  TodayViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/8/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TodayViewController.h"
#import "TAMacro.h"

#import "TodayDetailViewController.h"
#import "TodayCustomCollectionViewCell.h"
#import "AFNetworking.h"

#import "BaseModel.h"

#import "TodayButtonViewController.h"


#import "TodayRightItemViewController.h"

#import "TodayVideoCollectionViewCell.h"
#import "TodayVideoViewController.h"
@interface TodayViewController ()<UITableViewDataSource,UITableViewDelegate, UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UITableView *todayTable;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,copy) NSString *str;

@property (nonatomic,strong) UIButton *playButton1;

@end

@implementation TodayViewController


#pragma mark -------视图viewDidLoad方法
- (void)viewDidLoad {
    [super viewDidLoad];

    [self createNavigationItem];
    [self createTable];
    [self createCollection];
}



- (void)createTable
{
    _todayTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _todayTable.delegate = self;
    _todayTable.dataSource = self;
    [self.view addSubview:_todayTable];
    [_todayTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"todayCell"];
    _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _todayTable.tableHeaderView = _headerView;
}

- (void)createNavigationItem
{
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TitleLogo"]];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ButtonList"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItem)];
    self.navigationItem.rightBarButtonItem = barButtonItem;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ButtonHome"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClicked)];
    
    
}

- (void)leftButtonClicked
{
    _collection.contentOffset = CGPointMake(0, 0);
}

- (void)createCollection
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    [self.collection reloadData];
    [self netWorkGet];
    flow.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _collection = [[UICollectionView alloc] initWithFrame:SCREEN_RECT collectionViewLayout:flow];
    [_headerView addSubview:_collection];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.pagingEnabled = YES;
    _collection.delegate = self;
    _collection.dataSource = self;
    _collection.contentOffset = CGPointMake(_rowValue * SCREEN_WIDTH, 0);
    [_collection registerClass:[TodayCustomCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"];
    [_todayTable addSubview:_collection];
    
    [_collection registerClass:[TodayVideoCollectionViewCell class] forCellWithReuseIdentifier:@"videoCollectionViewCell"];
}



#pragma mark-------返回tableviewCell的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"todayCell"];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

#pragma mark -------返回tableview的item个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark -----返回tableview的item个数
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 1;
}

#pragma mark ----右侧按钮的点击方法
- (void)rightBarButtonItem
{
    TodayRightItemViewController *rightVC = [TodayRightItemViewController new];
    [self.navigationController pushViewController:rightVC animated:YES];
    rightVC.todayArray = _totayArr;
}

#pragma mark ---today网络请求
- (void)netWorkGet
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=15";
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject;
        _totayArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
           _esarray = [Esarray modelWithDic:dic];
            [_totayArr addObject:_esarray];
        }
        [self.collection reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}

#pragma mark - today集合视图的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _totayArr.count;
}




#pragma mark ------today集合视图返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
        TodayCustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
        NSInteger row = [indexPath row];

        Esarray *esarry = [_totayArr objectAtIndex:row];
        self.str = [esarry.link_video componentsSeparatedByString:@"#"].firstObject;
        NSString *str = [esarry cover_thumb_hd];



        [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:str]];
        NSArray *pubdateArr = [esarry.pubdate componentsSeparatedByString:@" "];
        NSString *day = [pubdateArr[1] substringToIndex:2];
        NSRange monthrange = NSMakeRange(0, 3);
        NSString *month = [[[esarry pubdate] substringWithRange:monthrange] uppercaseString];
//        cell.titieLabel.text = esarry.title;
        cell.titieLabel.text = esarry.title;
        cell.dayLabel.text = day;
        cell.monthLabel.text = month;
        cell.sourceLabel.text = esarry.source;

        if (indexPath.row == 1) {
        [cell.playButton addTarget:self action:@selector(playButtonClick:) forControlEvents:UIControlEventTouchUpInside];
       
        [cell.contentView addSubview:cell.playButton];


        }

        return cell;

}


- (void)playButtonClick:(UIButton *)playButtton1
{
    TodayVideoViewController *todayVideoVC = [TodayVideoViewController new];

    todayVideoVC.str = _str;
    [self.navigationController pushViewController:todayVideoVC animated:YES];
}






#pragma mark ------today集合视图的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    TodayDetailViewController *todayDetailVC = [TodayDetailViewController new];
    todayDetailVC.rowValue = indexPath.row;
    todayDetailVC.totayArr = _totayArr;
    
    [self.navigationController pushViewController:todayDetailVC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
